/* This file is part of SpaceSim.
 
 SpaceSim is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 SpaceSim is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with SpaceSim.  If not, see <http://www.gnu.org/licenses/>. */

#include "control.h"

#include "graphics.h"
#include "main.h"
#include "player.h"

void KeyHandler() {
	const sf::Input& Input = Graphics::App.GetInput();
	if (Input.IsKeyDown(sf::Key::Up)) { // accelerate
		player.Accelerate();
	}
	if (Input.IsKeyDown(sf::Key::Left)) { // turn
		player.TurnLeft();
	}
	if (Input.IsKeyDown(sf::Key::Right)) { // turn
		player.TurnRight();
	}
	if (Input.IsKeyDown(sf::Key::Down)) {	// reverse
		player.Reverse();
	}
	if (Input.IsKeyDown(sf::Key::A)) { // aim at someting
		player.Aim();
	}
}

void KeyPressedHandler(int key) {
	switch (key) {
		case sf::Key::Escape:
			if (Graphics::ViewStack.top() == Graphics::ViewMode::StationView)
				Main::Message.SetText("Departing from station.");
			if (Graphics::ViewStack.top() != Graphics::ViewMode::SystemView)
				Graphics::ViewStack.pop();	// close the top window
			break;
		/*case sf::Key::O:
			player.SetOrbitControl();
			break;
		case sf::Key::M:
			if (Graphics::ViewStack.top() == Graphics::ViewMode::SystemView)
				player.SetManualControl();
			break;
		case sf::Key::D:
			player.SetDockControl();
			break;*/
		case sf::Key::I:
			Graphics::ViewStack.push(Graphics::ViewMode::SystemInfoView);
			break;
		case sf::Key::L:
			if (Graphics::ViewStack.top() == Graphics::ViewMode::SystemView)
				player.TryToDock();
			break;
		case sf::Key::M:
			if (Graphics::ViewStack.top() == Graphics::ViewMode::StationView)
				player.SetMission(Mission());
			else
				Graphics::ViewStack.push(Graphics::ViewMode::MissionView);
			break;
		default:
			break;
	}
}

void MouseHandler() {
	
}

void MousePressedHandler(float x, float y, int button) {
	switch (Graphics::ViewStack.top()) {
		case Graphics::ViewMode::SystemInfoView:
			player.SetTargetPlanet((y - 50) / 30);
			break;
		case Graphics::ViewMode::WarpSelectView:
			//player.SetOrbitControl();
			Main::SubSystem = (y - 20) / 30;
			Graphics::ViewStack.pop();	// remove the warp select view
			Graphics::ViewStack.push(Graphics::ViewMode::SystemInfoView);
			break;
		case Graphics::ViewMode::WarpHubView:
			//player.SetOrbitControl();
			if (y > 40 && y < 70) {
				Main::CurrentCluster.PrevCluster();
			} else if (y > 70 && y < 100) {
				Main::CurrentCluster.NextCluster();
			}
			Graphics::ViewStack.pop();	// remove the warp select view
			Graphics::ViewStack.push(Graphics::ViewMode::SystemInfoView);
			break;
		default:
			break;
	}
}